<?php
/**
 * Created by PhpStorm.
 * User: zhaoyi
 * Date: 2022/1/26
 * Time: 11:46.
 */

declare(strict_types=1);

namespace App\Application\Api\Controller;

use App\Annotation\Api;
use App\Application\Admin\Model\Store;
use App\Application\Admin\Service\AdminSettingService;
use Hyperf\DbConnection\Db;
use Hyperf\Di\Annotation\Inject;
use Hyperf\HttpServer\Annotation\PostMapping;
use Hyperf\HttpServer\Annotation\GetMapping;
use Hyperf\HttpServer\Annotation\Controller;
use Swoole\Server;
#[Controller("/api/index/")]
class IndexController extends AbstractController
{
    #[Inject]
    protected AdminSettingService $setting;

    #[Api]
    #[PostMapping("postapi")]
    function postapi()
    {
        // $setting = $this->request->post('setting', []);
        $res = 1;

        return $res ? $this->returnSuccessJson(compact('res')) : $this->returnErrorJson();
    }

    #[Api]
    #[GetMapping("getapi")]
    function getapitest()
    {
        $setting = $this->request->query('*', []);

        $where = [];
        $where[] = ['end_time', '<', time()];
        $where[] = ['status', '=', 1];
        $order = Db::table('order')->where($where)->select('order_id')->first();
        if ($order) {
            return compact('order');
        } else {
            return compact('setting');
        }
        return compact('order');
    }

    /*
     *
     *首页公共
     * */
    #[Api]
    #[PostMapping("notice")]
    function notice()
    {
        $notice = $this->setting->getSiteSetting('notice');

        return $notice ? $this->returnSuccessJson(compact('notice')) : $this->returnErrorJson();
    }

    /*
     *
     *轮播图
     * */
    #[Api]
    #[PostMapping("AppInfo")]
    function AppInfo()
    {

        $setting = $this->setting->getSiteSetting('banner');
        //轮播图
        $banner = json_decode($setting);
        $alliance = DB::table('alliance')->inRandomOrder()->take(20)->get();
        //加盟信息
        return $banner ? $this->returnSuccessJson(compact('banner','alliance')) : $this->returnErrorJson();
    }

    /*
     *
     *city不city
     * */
    #[Api]
    #[PostMapping("city")]
    function city()
    {
        $where = [];
        $where[] = ['store_id', '>', 0];
        $lists = Store::where($where)
            ->orderBy('created_at', 'DESC')
            ->select('city_id')->get()->toArray();
        foreach ($lists as $k => $v) {

            $name = Db::table('region')->where('id', $v['city_id'])->value('merger_name');
            $namearr = explode(',', $name);

            $lists[$k]['city_name'] = isset($namearr[1]) ? $namearr[1] : $namearr[0];
            //   return $lists ? $this->returnSuccessJson(compact('lists')) : $this->returnErrorJson();
        }
        $lists = self::array_unset_tt($lists);
        return $lists ? $this->returnSuccessJson(compact('lists')) : $this->returnErrorJson();
    }


    /*
     *
     *city不city
     * */
    #[Api]
    #[PostMapping("citydata")]
    function citydata()
    {
        $pid = $this->request->input('pid', '');
        if ($pid) {
            $list = Db::table('region')->where('pid', $pid)->select('id', 'name', 'zip_code as citycode')->get();
        } else {
            $list = Db::table('region')->where('pid', 0)->select('id', 'name', 'zip_code as citycode')->get();
        }

        return $list ? $this->returnSuccessJson(compact('list')) : $this->returnErrorJson();
    }


    /*
     *
     *city不city
     * */
    #[Api]
    #[GetMapping("addressList")]
    function addressList()
    {
        $user = $this->checkToken();
        if (empty($user)) {
            return $this->returnErrorJson('请先登录');
        }
        $list = Db::table('region')->where('pid', 0)->select('id', 'name', 'zip_code')->get();
        foreach ($list as $v) {
            $zip_code = Db::table('region')->where('id', ($v->id) + 1)->orderBy('id', 'desc')->value('zip_code');
            Db::table('region')->where('id', $v->id)->update(['zip_code' => $zip_code]);
        }


        $list = Db::table('user_address')->where('user_id', $user['member_id'])->get();
        return $list ? $this->returnSuccessJson(compact('list')) : $this->returnErrorJson();
    }

    /*
 *
 *city不city
 * */
    #[Api]
    #[PostMapping("updateAddress")]
    function updateAddress()
    {


        $user = $this->checkToken();
        if (empty($user)) {
            return $this->returnErrorJson('请先登录');
        }
        $request = $this->request->all();
        $request['user_id'] = $user['member_id'];

        if ($request['id']) {

            $list = Db::table('user_address')->where('user_id', $user['member_id'])->update($request);
        } else {
            $list = Db::table('user_address')->insert($request);
        }

        return $list ? $this->returnSuccessJson(compact('request')) : $this->returnErrorJson();
    }


}